import store from "@/store"
/**
 * 根据组件的坐标获取对应的px值
 * @param { Style } style 
 * @returns 
 */
export function componentToPx(style){
  const {
    width: pageWidth,
    height: pageHeight
  } = store.state.lowCode.pageSetting
  const {
    leftUnit,
    topUnit,
    widthUnit,
    heightUnit
  } = style
  let {
    left,
    top,
    width,
    height,
  } = style
  if(leftUnit === "%"){
    left = left / 100 * pageWidth
  }
  if(topUnit === "%"){
    top = top / 100 * pageHeight
  }
  if(widthUnit === "%"){
    width = width / 100 * pageWidth
  }
  if(heightUnit === "%"){
    height = height / 100 * pageHeight
  }
  return {
    left,
    top,
    width,
    height,
  }
}
/**
 * 根据组件的坐标获取对应的%值
 * @param {*} style 
 * @returns 
 */
export function componentToPercentage(style){
  const {
    width: pageWidth,
    height: pageHeight
  } = store.state.lowCode.pageSetting
  const {
    leftUnit,
    topUnit,
    widthUnit,
    heightUnit
  } = style
  let {
    left,
    top,
    width,
    height,
  } = style
  if(leftUnit === "%"){
    left = left / pageWidth * 100
  }
  if(topUnit === "%"){
    top = top / pageHeight * 100
  }
  if(widthUnit === "%"){
    width = width / pageWidth * 100
  }
  if(heightUnit === "%"){
    height = height / pageHeight * 100
  }
  return {
    left,
    top,
    width,
    height,
  }
}